//
//  ICGtasksTableViewCell.m
//  ICome
//
//  Created by zhangrongwu on 16/6/3.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICGtasksTableViewCell.h"
#import "NSMutableAttributedString+Extension.h"
#import "ICHeaderFile.h"

@interface ICGtasksTableViewCell()

@property (nonatomic, strong)UIImageView *bgImageView;

@property (nonatomic, strong)UIImageView *headImageView;

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIView *sumContainerView;
@property (nonatomic, strong)UILabel *summaryLabel;

@property (nonatomic, strong)UIView *containerView;
@property (nonatomic, strong)UIButton *sureBtn;
@property (nonatomic, strong)UIButton *cancelBtn;
@property (nonatomic, strong)UIButton *detailBtn;

@property (nonatomic, strong)UIImageView *statusImageView;

@end
@implementation ICGtasksTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}

-(void)setup {
    self.bottomLineStyle = CellLineStyleNone;
    
    self.dateLabel = [[UILabel alloc] init];
    self.dateLabel.textColor = [UIColor whiteColor];
    self.dateLabel.font = [UIFont systemFontOfSize:12];
    self.dateLabel.layer.masksToBounds = YES;
    self.dateLabel.layer.cornerRadius = 5;
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    self.dateLabel.backgroundColor = IColor(197, 197, 197);
    
    self.bgImageView = [[UIImageView alloc] init];
    UIImage *bgimg = [[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_bg"] stretchableImageWithLeftCapWidth:40 topCapHeight:32];
    self.bgImageView.image = bgimg;
    
    self.headImageView = [[UIImageView alloc] init];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 20;
    self.headImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.sumContainerView = [[UIView alloc] init];
    self.sumContainerView.backgroundColor = [UIColor whiteColor];
    self.sumContainerView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.sumContainerView.layer.borderWidth = 0.5;
    
    self.summaryLabel = [[UILabel alloc] init];
    self.summaryLabel.textColor = ICTEXTCOLOR_T1;
    self.summaryLabel.font = [UIFont systemFontOfSize:14];
    self.summaryLabel.backgroundColor = [UIColor whiteColor];
    self.summaryLabel.numberOfLines = 0;
    [self.summaryLabel sizeToFit];
    [self.sumContainerView addSubview:self.summaryLabel];
    
    self.containerView = [[UIView alloc] init];
    self.containerView.userInteractionEnabled = YES;
    
    [self sd_addSubviews:@[self.dateLabel, self.bgImageView, self.headImageView, self.titleLabel, self.sumContainerView, self.containerView]];
    
    self.sureBtn = [[UIButton alloc] init];
    [self.sureBtn setTitle:@"同意" forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sureBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_left_N"] forState:UIControlStateNormal];
    [self.sureBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_left_S"] forState:UIControlStateSelected];
    self.sureBtn.hidden = YES;
    [self.sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:_sureBtn];
    
    self.cancelBtn = [[UIButton alloc] init];
    [self.cancelBtn setTitle:@"驳回" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_right_N"] forState:UIControlStateNormal];
    [self.cancelBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_right_S"] forState:UIControlStateSelected];
    self.cancelBtn.hidden = YES;
    [self.cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:_cancelBtn];
    
    self.detailBtn = [[UIButton alloc] init];
    [self.detailBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [self.detailBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_N"] forState:UIControlStateNormal];
    [self.detailBtn setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ctastks_btnbg_S"] forState:UIControlStateSelected];
    self.detailBtn.hidden = YES;
    [self.detailBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.detailBtn addTarget:self action:@selector(detailBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:_detailBtn];
    
    self.statusImageView = [[UIImageView alloc] init];
    self.statusImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_gtasks_agree"];
    self.statusImageView.hidden = YES;
    [self.containerView addSubview:self.statusImageView];
}

- (void)sureClick {
    if ([self.delegate respondsToSelector:@selector(didClickPendingSureBtnInCell:)]) {
        [self.delegate didClickPendingSureBtnInCell:self];
    }
}

- (void)cancelClick {
    if ([self.delegate respondsToSelector:@selector(didClickPendingCancelBtnInCell:)]) {
        [self.delegate didClickPendingCancelBtnInCell:self];
    }
}

- (void)detailBtnClick {
    if ([self.delegate respondsToSelector:@selector(didClickPendingDetailBtnInCell:)]) {
        [self.delegate didClickPendingDetailBtnInCell:self];
    }
}

- (void)layoutSubview {
    CGFloat conterLeftSpacing = 9;
    
    [self.dateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(130);
    }];
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        // 需根据内容判断处理
        make.top.equalTo(self.dateLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView.mas_left).offset(20);
        make.height.and.width.mas_equalTo(40);
    }];
    
    [self.bgImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLabel.mas_bottom).offset(10);
        make.left.equalTo(self.headImageView.mas_right).offset(-5);
        make.right.equalTo(self.contentView.mas_right).offset(-50);
        make.height.mas_equalTo(40);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgImageView.mas_left).offset(15);
        make.top.equalTo(self.bgImageView.mas_top).offset(10);
    }];
    
    [self.sumContainerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.mas_bottom);
        make.left.equalTo(self.bgImageView.mas_left).offset(conterLeftSpacing);
        make.right.equalTo(self.bgImageView.mas_right);
        make.bottom.equalTo(self.summaryLabel.mas_bottom);
    }];
    
    [self.summaryLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.sumContainerView.mas_top).offset(5);
        make.right.equalTo(self.sumContainerView.mas_right).offset(-1);
    }];
    
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sumContainerView.mas_bottom).offset(-0.5);
        make.left.equalTo(self.bgImageView.mas_left).offset(conterLeftSpacing);
        make.right.equalTo(self.bgImageView.mas_right);
        make.height.equalTo(@50);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    [self.detailBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView.mas_left);
        make.top.equalTo(self.containerView.mas_top);
        make.width.mas_equalTo(self.containerView.mas_width);
        make.height.equalTo(@50);
    }];
    
    [self.sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView.mas_left);
        make.top.equalTo(self.containerView.mas_top);
        make.width.mas_equalTo((App_Frame_Width - 115) / 2);
        make.height.equalTo(@50);
    }];
    
    [self.cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView.mas_top);
        make.left.equalTo(self.sureBtn.mas_right);
        make.width.mas_equalTo((App_Frame_Width - 115) / 2 + 1);
        make.height.equalTo(@50);
    }];
    
    [self.statusImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.containerView.mas_right).offset(-1);
        make.bottom.equalTo(self.cancelBtn.mas_bottom).offset(-1);
    }];
}

-(void)setModel:(ICGtasksModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    if (![ICTools stringEmpty:model.summary]) {
        self.summaryLabel.attributedText = [NSMutableAttributedString setString:model.summary LineSpace:5 characterSpacing:0 textAlignment:NSTextAlignmentLeft];
    }
    self.dateLabel.text = [model _createDate];
    
    if (![model.status isEqualToString:@"0"]) {
        // 已办
        if ([model.status isEqualToString:@"1"]) {
            self.statusImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_gtasks_agree"];
        } else {
            self.statusImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_gtasks_disagree"];
        }
        
        self.sureBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        
        self.detailBtn.hidden = NO;
        self.statusImageView.hidden = NO;
    } else {
        // 未办
        self.sureBtn.hidden = NO;
        self.cancelBtn.hidden = NO;
        
        self.detailBtn.hidden = YES;
        self.statusImageView.hidden = YES;
    }
    NSString *url = [ICTools getMinImageURL:model.msgId];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];

    [self layoutSubview];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)sd_addSubviews:(NSArray *)subviews {
    [subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        if ([view isKindOfClass:[UIView class]]) {
            [self.contentView addSubview:view];
        }
    }];
}

@end
